package com.verisette.ncos.web.approver.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.corp.agi.common.bean.LabelValueBean;
import org.corp.agi.common.bean.PagingBean;
import org.corp.agi.service.bean.ServiceRequest;
import org.corp.agi.service.bean.ServiceResponse;
import org.corp.agi.web.action.BaseAction;
import org.corp.agi.web.util.ServiceRequestFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.Action;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.CustomCustomerOrder;
import com.verisette.ncos.service.approver.gateway.ApproverService;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class CardApproverAction extends BaseAction 
{
	private Logger LOG = Logger.getLogger(CardApproverAction.class);

	private ApproverService approverService;
	@Autowired
	@Required
	public void setApproverService(ApproverService approverService) {
		this.approverService = approverService;
	}
	
	private List<CustomCustomerOrder> customerOrderList;
	private PagingBean pagingBean;
	private String orderId;
	private String pvPath_en;
	private String pvPath_th;
	private String reason;
	private CustomCustomerOrder custOrder;
	private String pdfPath;
	
	public String init() throws Exception 
	{
		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing init()...");
		}
		
		httpServletRequest.getSession().setAttribute(ProjectConstant.SESSION_MENU_PAGE, ProjectConstant.MENU_PAGE_APPROVED01);
		
		pagingBean = new PagingBean(ProjectConstant.ROW_PER_PAGE);
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		if(ProjectConstant.MENU_PAGE_APPROVED02.equals(httpServletRequest.getSession().getAttribute(ProjectConstant.SESSION_MENU_PAGE))){
			req.setValueObject(ProjectConstant.STATUS_APPROVE_Y);
		}
		req.setPaging(pagingBean);
		
		ServiceResponse<List<CustomCustomerOrder>> serviceResponse = approverService.inquiryCustomerOrderList(req);
		
		if(isServiceResponseSuccess(serviceResponse))
		{
			customerOrderList = serviceResponse.getValueObject();
			pagingBean = serviceResponse.getPaging();
			
			if(null != customerOrderList && customerOrderList.size()>0)
			{
				custOrder = customerOrderList.get(0);
			}
		}
		
		return Action.SUCCESS;
	}

	public String init2() throws Exception 
	{
		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing init()...");
		}
		
		httpServletRequest.getSession().setAttribute(ProjectConstant.SESSION_MENU_PAGE, ProjectConstant.MENU_PAGE_APPROVED02);
		
		pagingBean = new PagingBean(ProjectConstant.ROW_PER_PAGE);
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		req.setValueObject(ProjectConstant.STATUS_APPROVE_Y);
		req.setPaging(pagingBean);
		
		ServiceResponse<List<CustomCustomerOrder>> serviceResponse = approverService.inquiryCustomerOrderList(req);
		
		if(isServiceResponseSuccess(serviceResponse))
		{
			customerOrderList = serviceResponse.getValueObject();
			pagingBean = serviceResponse.getPaging();
			
			if(null != customerOrderList && customerOrderList.size()>0)
			{
				custOrder = customerOrderList.get(0);
			}
		}
		
		return Action.SUCCESS;
	}
	
	public String inquiry()
	{
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);

		if(ProjectConstant.MENU_PAGE_APPROVED02.equals(httpServletRequest.getSession().getAttribute(ProjectConstant.SESSION_MENU_PAGE))){
			req.setValueObject(ProjectConstant.STATUS_APPROVE_Y);
		}
		
		if(LOG.isDebugEnabled()){
			StringBuilder sb = new StringBuilder();
			sb.append("\n\t\t********* PagingBean : ");
			sb.append("\n\t\tPageCount=" + pagingBean.getPageCount());
			sb.append("\n\t\tCurrentPage=" + pagingBean.getCurrentPage());
			sb.append("\n\t\tTotalRows=" + pagingBean.getTotalRows());
			LOG.debug(sb.toString());
		}
		
		req.setPaging(pagingBean);
		
		ServiceResponse<List<CustomCustomerOrder>> serviceResponse = approverService.inquiryCustomerOrderList(req);
		
		if(isServiceResponseSuccess(serviceResponse))
		{
			customerOrderList = serviceResponse.getValueObject();
			pagingBean = serviceResponse.getPaging();
		}
		
		return Action.SUCCESS;
	}
	
	public String preview(){
		return Action.SUCCESS;
	}
	
	public String approve()
	{
		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing approve()...");
		}
		
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		req.setValueObject(orderId);
		
		ServiceResponse<CustomCustomerOrder> res = approverService.getCustomerOrderById(req);
		
		if(isServiceResponseSuccess(res))
		{
			custOrder = res.getValueObject();
			if(LOG.isDebugEnabled()) LOG.debug(custOrder);
		}
		
		return "approve";
	}
	

	public String submitApprove()
	{
		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing submitApprove()...");
		}
		
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		req.setValueObject(orderId);
		
		ServiceResponse<CustomCustomerOrder> res = approverService.submitApprove(req);
		
		if(isServiceResponseSuccess(res))
		{
			custOrder = res.getValueObject();
			if(LOG.isDebugEnabled()) LOG.debug(custOrder);
		}
		
		return "successApprove";
	}
	
	public String notApprove()
	{
		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing notApprove()...");
		}
		
		return "notApprove";
	}
	
	public String submitNotApprove()
	{
		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing submitNotApprove()...");
		}
		
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		req.setValueObject(new LabelValueBean(orderId, reason));
		
		ServiceResponse<CustomCustomerOrder> res = approverService.submitNotApprove(req);
		
		if(isServiceResponseSuccess(res))
		{
			custOrder = res.getValueObject();
		}
		
		return "successNotApprove";
	}
	
	public String back()
	{
		orderId = "";
		reason = "";
		pvPath_en = "";
		pvPath_th = "";
		
		if("Y".equals(httpServletRequest.getParameter("approveCompletePage"))){
			if(pagingBean.getPageCount() == pagingBean.getCurrentPage() && (pagingBean.getTotalRows() % ProjectConstant.ROW_PER_PAGE) == 1){
				if(pagingBean.getPageCount()>1) pagingBean.setCurrentPage(pagingBean.getCurrentPage()-1);
			}
		}
		
		return inquiry();
	}

	public List<CustomCustomerOrder> getCustomerOrderList() {
		return customerOrderList;
	}

	public void setCustomerOrderList(List<CustomCustomerOrder> customerOrderList) {
		this.customerOrderList = customerOrderList;
	}

	public PagingBean getPagingBean() {
		return pagingBean;
	}

	public void setPagingBean(PagingBean pagingBean) {
		this.pagingBean = pagingBean;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public CustomCustomerOrder getCustOrder() {
		return custOrder;
	}

	public void setCustOrder(CustomCustomerOrder custOrder) {
		this.custOrder = custOrder;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public String getPvPath_en() {
		return pvPath_en;
	}

	public void setPvPath_en(String pvPath_en) {
		this.pvPath_en = pvPath_en;
	}

	public String getPvPath_th() {
		return pvPath_th;
	}

	public void setPvPath_th(String pvPath_th) {
		this.pvPath_th = pvPath_th;
	}

	public String getPdfPath() {
		return pdfPath;
	}

	public void setPdfPath(String pdfPath) {
		this.pdfPath = pdfPath;
	}
	
}
